package top.tenebrous.yzl.instance;

import top.tenebrous.yzl.basic.utils.DateUtils;
import top.tenebrous.yzl.enums.InstanceStatusEnum;
import top.tenebrous.yzl.model.request.JesticAuthorityRequest;
import top.tenebrous.yzl.utils.InstanceUtils;

import java.util.Date;

/**
 * @Description:
 * @Project: jestic-server
 * @Package: com.tenebrous.jestic.server
 * @Author: zhangya
 * @CreateTime: 2023-06-03  13:28
 * @Version: 1.0.0
 */
public abstract class Instance {

    /**实例id 创建时间+随机数5位  唯一**/
    private String InstanceId;

    /**服务签名 服务端创建服务时生成 用户服务唯一性查询 每个服务唯一  一个签名可能对应多个实例**/
    private String serverSign;

    /**服务名称**/
    private String serverName;

    /**创建时间 实例认证时间**/
    private Long createTime;

    /**实例过期时间 服务token到期时间 到期后需要重新续签token**/
    private Long expireTime = 0L;

    /**实例最近一次存活时间 实例每分钟会向服务端发送一次心跳，服务端每收到一次心跳会记录一次存活时间，当服务端2分钟没有收到心跳，会在第三分钟后下线服务**/
    private Long recentlyAuth;

    /**实例状态**/
    private InstanceStatusEnum instanceStatus;

    /**实例心跳次数**/
    private int times;

    /**实例token**/
    private String token;

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getGrpcPort() {
        return grpcPort;
    }

    public void setGrpcPort(String grpcPort) {
        this.grpcPort = grpcPort;
    }

    /**实例环境**/
    private String env;

    private String ip;

    private String port;

    private String grpcPort;

    /**实例上下文**/
    private final AbstractInstanceContext instanceContext;

    public Instance(AbstractInstanceContext instanceContext) {
        this.instanceContext = instanceContext;
    }

    /**
     * @description:
     *  初始化实例对象
     *
     * @author: zhangya
     * @date: 2023/6/4 9:27
     * @param:
     * @return:
     **/
    public Instance initInstance(JesticAuthorityRequest request) {
        this.InstanceId = InstanceUtils.createInstanceId();
        this.serverName = instanceContext.getJesticServer().getServerName();
        this.serverSign = instanceContext.getJesticServer().getServerSign();
        this.createTime = new Date().getTime();
        // 过期时间 24 小时
        this.expireTime =  DateUtils.addDateTimeByMinute(new Date(), 60 * 24).getTime();
        this.recentlyAuth = new Date().getTime();
        this.instanceStatus = InstanceStatusEnum.INIT;
        this.token = instanceContext.getAuthTokenGenerator().generateToken(InstanceId);
        this.env = instanceContext.getJesticServer().getEnv();
        // 设置客户端信息
        setIp(request.getIp());
        setPort(request.getPort());
        setGrpcPort(request.getGrpcPort());
        return this;
    }

    /**
     * @description:
     * 注册实例
     *
     * @author: zhangya
     * @date: 2023/6/4 9:27
     * @param:
     * @return:
     **/
    public void registerInstance(Instance instance) {
        instanceContext.registerInstance(instance);
    }

    public void flushInstance(Instance instance) {
        instanceContext.flushInstance(instance);
    }

    public String getInstanceId() {
        return InstanceId;
    }

    public void setInstanceId(String instanceId) {
        InstanceId = instanceId;
    }

    public String getServerSign() {
        return serverSign;
    }

    public void setServerSign(String serverSign) {
        this.serverSign = serverSign;
    }

    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    public Long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Long createTime) {
        this.createTime = createTime;
    }

    public Long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    public Long getRecentlyAuth() {
        return recentlyAuth;
    }

    public void setRecentlyAuth(Long recentlyAuth) {
        this.recentlyAuth = recentlyAuth;
    }

    public InstanceStatusEnum getInstanceStatus() {
        return instanceStatus;
    }

    public void setInstanceStatus(InstanceStatusEnum instanceStatus) {
        this.instanceStatus = instanceStatus;
    }

    public int getTimes() {
        return times;
    }

    public void setTimes(int times) {
        this.times = times;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getEnv() {
        return env;
    }

    public void setEnv(String env) {
        this.env = env;
    }

}
